// 收货地址编辑 新建 逻辑
var _mm = require('util/mm.js')
var _address = require('service/address-service.js')
var templateAddressModal = require('./address-modal.string')
var _cities = require('util/cities/index.js')

var addressModal = {
    show: function (option) {
        //    option的绑定
        this.option = option;
        this.option.data = option.data || {};
        this.$modalWrap = $('.modal-wrap');
        // 渲染页面
        this.loadModal();
        this.bindEvent();
        this.getSelectOption()
    },
    loadModal: function () {
        var addressModalHtml = _mm.renderHtml(templateAddressModal,{
            isUpdate : this.option.isUpdate,
            data     : this.option.data
        })
        this.$modalWrap.html(addressModalHtml)
        // 加载省份 城市
        this.loadProvince()
    },
    // 加载省份信息
    loadProvince: function () {
        var province = _cities.getProvinces() || [];
        var $provinceSelect = this.$modalWrap.find('#receiver-province');
        $provinceSelect.html(this.getSelectOption(province));
        // 如果是更新地址并且有省份信息 做回填
        if(this.option.isUpdate && this.option.data.receiverProvince){
            $provinceSelect.val(this.option.data.receiverProvince)
            this.loadCity(this.option.data.receiverProvince)
        }
    },
    //获取select的选项 输入array,输出html
    getSelectOption: function (optionArray) {
        var html = '<option value="">请选择</option>'
        for (var i = 0, length = optionArray.length; i < length; i++) {
            html += '<option value="' + optionArray[i] + '">' + optionArray[i] + '</option>';
        }
        return html
    },
    // 加载城市信息
    loadCity: function (provinceName) {
        var cities = _cities.getCities(provinceName) || [],
            $citySelect = this.$modalWrap.find('#receiver-city');
        $citySelect.html(this.getSelectOption(cities))
        // 如果是更新地址并且有城市信息 做回填
        if(this.option.isUpdate && this.option.data.receiverCity){
           $citySelect.val(this.option.data.receiverCity)
        }
    },
    bindEvent: function () {
        //给省加载onchange事件 省份是城市的二级联动
        var _this = this;
        this.$modalWrap.find('#receiver-province').on('change', function () {
            var selectProvince = $(this).val()
            _this.loadCity(selectProvince)
        })
        // 提交收货地址
        this.$modalWrap.find('.address-btn').on('click', function (e) {
            window.event ? window.event.returnValue = false : e.preventDefault();
            // 获取表单信息
            var receiverInfo = _this.getReceiverInfo(),
                isUpdate = _this.option.isUpdate;
            // 使用新地址(不是更新地址) 且 验证通过
            if (!isUpdate && receiverInfo.status) {
                _address.save(receiverInfo.data, function (res) {
                    _mm.successTips('地址添加成功')
                    // 关闭添加地址的弹窗
                    _this.hide()
                    // 判断传进来的onSuccess是不是个function &&=>调用的意思
                    typeof _this.option.onSuccess === 'function' &&
                        _this.option.onSuccess();
                }, function (errMsg) {
                    _mm.errorTips(errMsg)
                });
            }
            // 更新收件人地址 且验证通过
            else if (isUpdate && receiverInfo.status) {
                _address.update(receiverInfo.data,function(res){
                    _mm.successTips('地址更新成功')
                    // 关闭添加地址的弹窗
                    _this.hide()
                    // 判断传进来的onSuccess是不是个function &&=>调用的意思
                    typeof _this.option.onSuccess === 'function' &&
                        _this.option.onSuccess();
                },function(errMsg){
                    _mm.errorTips(errMsg)
                })
            }
            //验证不通过
            else {
                _mm.errorTips(receiverInfo.errMsg || '好像哪里不对了')
            }
        });

        // 为了保证点击到内容区的时候不关闭弹窗 阻止冒泡
        this.$modalWrap.find('.modal-container').on('click', function (e) {
            window.event ? window.event.cancelBubble = true : e.stopPropagation()
            })
        // 点击x或者蒙版区域关闭弹窗
        this.$modalWrap.find('.close').on('click', function () {
            _this.hide()
        })


    },
    // 获取表单里收件人信息 并做表单的验证
    getReceiverInfo: function () {
        var result = {
                status: false
            },
            receiverInfo = {};
        receiverInfo.receiverName = $.trim(this.$modalWrap.find('#receiver-name').val());
        receiverInfo.receiverProvince = $.trim(this.$modalWrap.find('#receiver-province').val());
        receiverInfo.receiverCity = $.trim(this.$modalWrap.find('#receiver-city').val());
        receiverInfo.receiverAddress = $.trim(this.$modalWrap.find('#receiver-address').val());
        receiverInfo.receiverPhone = $.trim(this.$modalWrap.find('#receiver-phone').val());
        receiverInfo.receiverZip = $.trim(this.$modalWrap.find('#receiver-zip').val());
            // 如果是更新状态的话 把隐藏表单存放的地址的id拿出来 一起发送出去 不然不知道更新的是哪个地址
        if(this.option.isUpdate){
             receiverInfo.id = $.trim(this.$modalWrap.find('#receiver-id').val());
        }
        // 表单的验证
        if (!receiverInfo.receiverName) {
            result.errMsg = "请输入收件人姓名"
        } else if (!receiverInfo.receiverProvince) {
            result.errMsg = "请选择收件人所在省份"
        } else if (!receiverInfo.receiverCity) {
            result.errMsg = "请选择收件人所在城市"
        } else if (!receiverInfo.receiverPhone) {
            result.errMsg = "请输入收件人手机号"
        } else if (!receiverInfo.receiverAddress) {
            result.errMsg = "请输入详细地址"
        }
        //所有验证都通过了
        else {
            result.status = true;
            result.data = receiverInfo;
        }
        return result;
    },
    // 关闭弹窗
    hide: function () {
        this.$modalWrap.empty()
    }


}

module.exports = addressModal;